# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\gevent\monkey.py
"""
Make the standard library cooperative.

Patching
========

The primary purpose of this module is to carefully patch, in place,
portions of the standard library with gevent-friendly functions that
behave in the same way as the original (at least as closely as possible).

The primary interface to this is the :func:`patch_all` function, which
performs all the available patches. It accepts arguments to limit the
patching to certain modules, but most programs **should** use the
default values as they receive the most wide-spread testing, and some monkey
patches have dependencies on others.

Patching **should be done as early as possible** in the lifecycle of the
program. For example, the main module (the one that tests against
``__main__`` or is otherwise the first imported) should begin with
this code, ideally before any other imports::

    from gevent import monkey
    monkey.patch_all()

.. tip::

    Some frameworks, such as gunicorn, handle monkey-patching for you.
    Check their documentation to be sure.

Querying
--------

Sometimes it is helpful to know if objects have been monkey-patched, and in
advanced cases even to have access to the original standard library functions. This
module provides functions for that purpose.

- :func:`is_module_patched`
- :func:`is_object_patched`
- :func:`get_original`

Use as a module
===============

Sometimes it is useful to run existing python scripts or modules that
were not built to be gevent aware under gevent. To do so, this module
can be run as the main module, passing the script and its arguments.
For details, see the :func:`main` function.

Functions
=========
"""
from __future__ import absolute_import
from __future__ import print_function
import sys

__all__ = [
    "patch_all",
    "patch_builtins",
    "patch_dns",
    "patch_os",
    "patch_select",
    "patch_signal",
    "patch_socket",
    "patch_ssl",
    "patch_subprocess",
    "patch_sys",
    "patch_thread",
    "patch_time",
    "get_original",
    "is_module_patched",
    "is_object_patched",
    "main",
]
if sys.version_info[0] >= 3:
    string_types = (str,)
    PY3 = True
else:
    import __builtin__

    string_types = (__builtin__.basestring,)
    PY3 = False
WIN = sys.platform.startswith("win")
saved = {}


def is_module_patched(modname):
    """Check if a module has been replaced with a cooperative version."""
    return modname in saved


def is_object_patched(modname, objname):
    """Check if an object in a module has been replaced with a cooperative version."""
    return is_module_patched(modname) and objname in saved[modname]


def _get_original(name, items):
    d = saved.get(name, {})
    values = []
    module = None
    for item in items:
        if item in d:
            values.append(d[item])
        else:
            if module is None:
                module = __import__(name)
            values.append(getattr(module, item))

    return values


def get_original(mod_name, item_name):
    """Retrieve the original object from a module.
    
    If the object has not been patched, then that object will still be retrieved.
    
    :param item_name: A string or sequence of strings naming the attribute(s) on the module
        ``mod_name`` to return.
    :return: The original value if a string was given for ``item_name`` or a sequence
        of original values if a sequence was passed.
    """
    if isinstance(item_name, string_types):
        return _get_original(mod_name, [item_name])[0]
    else:
        return _get_original(mod_name, item_name)


_NONE = object()


def patch_item(module, attr, newitem):
    olditem = getattr(module, attr, _NONE)
    if olditem is not _NONE:
        saved.setdefault(module.__name__, {}).setdefault(attr, olditem)
    setattr(module, attr, newitem)


def remove_item(module, attr):
    olditem = getattr(module, attr, _NONE)
    if olditem is _NONE:
        return
    saved.setdefault(module.__name__, {}).setdefault(attr, olditem)
    delattr(module, attr)


def patch_module(name, items=None):
    gevent_module = getattr(__import__("gevent." + name), name)
    module_name = getattr(gevent_module, "__target__", name)
    module = __import__(module_name)
    if items is None:
        items = getattr(gevent_module, "__implements__", None)
        if items is None:
            raise AttributeError("%r does not have __implements__" % gevent_module)
    for attr in items:
        patch_item(module, attr, getattr(gevent_module, attr))

    return module


def _queue_warning(message, _warnings):
    if _warnings is None:
        _process_warnings([message])
    else:
        _warnings.append(message)


def _process_warnings(_warnings):
    import warnings

    for warning in _warnings:
        warnings.warn(warning, RuntimeWarning, stacklevel=3)


def _patch_sys_std(name):
    from gevent.fileobject import FileObjectThread

    orig = getattr(sys, name)
    if not isinstance(orig, FileObjectThread):
        patch_item(sys, name, FileObjectThread(orig))


def patch_sys(stdin=True, stdout=True, stderr=True):
    """Patch sys.std[in,out,err] to use a cooperative IO via a threadpool.
    
    This is relatively dangerous and can have unintended consequences such as hanging
    the process or `misinterpreting control keys`_ when ``input`` and ``raw_input``
    are used.
    
    This method does nothing on Python 3. The Python 3 interpreter wants to flush
    the TextIOWrapper objects that make up stderr/stdout at shutdown time, but
    using a threadpool at that time leads to a hang.
    
    .. _`misinterpreting control keys`: https://github.com/gevent/gevent/issues/274
    """
    if PY3:
        return
    if stdin:
        _patch_sys_std("stdin")
    if stdout:
        _patch_sys_std("stdout")
    if stderr:
        _patch_sys_std("stderr")


def patch_os():
    """
    Replace :func:`os.fork` with :func:`gevent.fork`, and, on POSIX,
    :func:`os.waitpid` with :func:`gevent.os.waitpid` (if the
    environment variable ``GEVENT_NOWAITPID`` is not defined). Does
    nothing if fork is not available.
    
    .. caution:: This method must be used with :func:`patch_signal` to have proper SIGCHLD
         handling and thus correct results from ``waitpid``.
         :func:`patch_all` calls both by default.
    
    .. caution:: For SIGCHLD handling to work correctly, the event loop must run.
         The easiest way to help ensure this is to use :func:`patch_all`.
    """
    patch_module("os")


def patch_time():
    """Replace :func:`time.sleep` with :func:`gevent.sleep`."""
    from gevent.hub import sleep
    import time

    patch_item(time, "sleep", sleep)


def _patch_existing_locks(threading):
    if len(list(threading.enumerate())) != 1:
        return
        try:
            tid = threading.get_ident()
        except AttributeError:
            tid = threading._get_ident()

        rlock_type = type(threading.RLock())
        try:
            import importlib._bootstrap
        except ImportError:

            class _ModuleLock(object):
                pass

        else:
            _ModuleLock = importlib._bootstrap._ModuleLock

        gc = __import__("gc")
        for o in gc.get_objects():
            if isinstance(o, rlock_type):
                pass
            if hasattr(o, "_owner"):
                if o._owner is not None:
                    o._owner = tid
                else:
                    if o._RLock__owner is not None:
                        o._RLock__owner = tid
            elif isinstance(o, _ModuleLock):
                if o.owner is not None:
                    o.owner = tid


def patch_thread(
    threading=True,
    _threading_local=True,
    Event=False,
    logging=True,
    existing_locks=True,
    _warnings=None,
):
    """
    Replace the standard :mod:`thread` module to make it greenlet-based.
    
    - If *threading* is true (the default), also patch ``threading``.
    - If *_threading_local* is true (the default), also patch ``_threading_local.local``.
    - If *logging* is True (the default), also patch locks taken if the logging module has
      been configured.
    - If *existing_locks* is True (the default), and the process is still single threaded,
      make sure than any :class:`threading.RLock` (and, under Python 3, :class:`importlib._bootstrap._ModuleLock`)
      instances that are currently locked can be properly unlocked.
    
    .. caution::
        Monkey-patching :mod:`thread` and using
        :class:`multiprocessing.Queue` or
        :class:`concurrent.futures.ProcessPoolExecutor` (which uses a
        ``Queue``) will hang the process.
    
    .. versionchanged:: 1.1b1
        Add *logging* and *existing_locks* params.
    """
    if threading:
        threading_mod = __import__("threading")
        orig_current_thread = threading_mod.current_thread()
    else:
        threading_mod = None
        orig_current_thread = None
    patch_module("thread")
    if threading:
        patch_module("threading")
        if Event:
            from gevent.event import Event

            patch_item(threading_mod, "Event", Event)
        if existing_locks:
            _patch_existing_locks(threading_mod)
        if logging:
            if "logging" in sys.modules:
                logging = __import__("logging")
                patch_item(logging, "_lock", threading_mod.RLock())
                for wr in logging._handlerList:
                    handler = wr() if callable(wr) else wr
                    if handler is None:
                        continue
                    if not hasattr(handler, "lock"):
                        raise TypeError("Unknown/unsupported handler %r" % handler)
                    handler.lock = threading_mod.RLock()

    if _threading_local:
        _threading_local = __import__("_threading_local")
        from gevent.local import local

        patch_item(_threading_local, "local", local)

    def make_join_func(thread, thread_greenlet):
        from gevent.hub import sleep
        from time import time

        def join(timeout=None):
            end = None
            if threading_mod.current_thread() is thread:
                raise RuntimeError("Cannot join current thread")
            if thread_greenlet is not None:
                if thread_greenlet.dead:
                    return
                if not thread.is_alive():
                    return
                if timeout:
                    end = time() + timeout
                while thread.is_alive():
                    if end is not None:
                        if time() > end:
                            return
                        sleep(0.01)

        return join

    if threading:
        from gevent.threading import main_native_thread

        for thread in threading_mod._active.values():
            if thread == main_native_thread():
                continue
            thread.join = make_join_func(thread, None)

    if sys.version_info[:2] >= (3, 4):
        greenlet = __import__("greenlet")
        if orig_current_thread == threading_mod.main_thread():
            main_thread = threading_mod.main_thread()
            _greenlet = main_thread._greenlet = greenlet.getcurrent()
            main_thread.join = make_join_func(main_thread, _greenlet)
            oldid = main_thread.ident
            main_thread._ident = threading_mod.get_ident()
            if oldid in threading_mod._active:
                threading_mod._active[main_thread.ident] = threading_mod._active[oldid]
            if oldid != main_thread.ident:
                del threading_mod._active[oldid]
            else:
                _queue_warning(
                    "Monkey-patching not on the main thread; threading.main_thread().join() will hang from a greenlet",
                    _warnings,
                )


def patch_socket(dns=True, aggressive=True):
    """Replace the standard socket object with gevent's cooperative sockets.
    
    If ``dns`` is true, also patch dns functions in :mod:`socket`.
    """
    from gevent import socket

    if dns:
        items = socket.__implements__
    else:
        items = set(socket.__implements__) - set(socket.__dns__)
    patch_module("socket", items=items)
    if aggressive:
        if "ssl" not in socket.__implements__:
            remove_item(socket, "ssl")


def patch_dns():
    """Replace DNS functions in :mod:`socket` with cooperative versions.
    
    This is only useful if :func:`patch_socket` has been called and is done automatically
    by that method if requested.
    """
    from gevent import socket

    patch_module("socket", items=socket.__dns__)


def patch_ssl():
    """Replace SSLSocket object and socket wrapping functions in :mod:`ssl` with cooperative versions.
    
    This is only useful if :func:`patch_socket` has been called.
    """
    patch_module("ssl")


def patch_select(aggressive=True):
    """
    Replace :func:`select.select` with :func:`gevent.select.select`
    and :func:`select.poll` with :class:`gevent.select.poll` (where available).
    
    If ``aggressive`` is true (the default), also remove other
    blocking functions from :mod:`select` and (on Python 3.4 and
    above) :mod:`selectors`:
    
    - :func:`select.epoll`
    - :func:`select.kqueue`
    - :func:`select.kevent`
    - :func:`select.devpoll` (Python 3.5+)
    - :class:`selectors.EpollSelector`
    - :class:`selectors.KqueueSelector`
    - :class:`selectors.DevpollSelector` (Python 3.5+)
    """
    patch_module("select")
    if aggressive:
        select = __import__("select")
        remove_item(select, "epoll")
        remove_item(select, "kqueue")
        remove_item(select, "kevent")
        remove_item(select, "devpoll")
    if sys.version_info[:2] >= (3, 4):
        select = __import__("select")
        orig_select_select = get_original("select", "select")
        if not select.select is not orig_select_select:
            raise AssertionError
        selectors = __import__("selectors")
        if selectors.SelectSelector._select in (select.select, orig_select_select):

            def _select(self, *args, **kwargs):
                return select.select(*args, **kwargs)

            selectors.SelectSelector._select = _select
            _select._gevent_monkey = True
        if aggressive:
            remove_item(selectors, "EpollSelector")
            remove_item(selectors, "KqueueSelector")
            remove_item(selectors, "DevpollSelector")
            selectors.DefaultSelector = selectors.SelectSelector


def patch_subprocess():
    """
    Replace :func:`subprocess.call`, :func:`subprocess.check_call`,
    :func:`subprocess.check_output` and :class:`subprocess.Popen` with
    :mod:`cooperative versions <gevent.subprocess>`.
    
    .. note::
       On Windows under Python 3, the API support may not completely match
       the standard library.
    
    """
    patch_module("subprocess")


def patch_builtins():
    """
    Make the builtin __import__ function `greenlet safe`_ under Python 2.
    
    .. note::
       This does nothing under Python 3 as it is not necessary. Python 3 features
       improved import locks that are per-module, not global.
    
    .. _greenlet safe: https://github.com/gevent/gevent/issues/108
    
    """
    if sys.version_info[:2] < (3, 3):
        patch_module("builtins")


def patch_signal():
    """
    Make the signal.signal function work with a monkey-patched os.
    
    .. caution:: This method must be used with :func:`patch_os` to have proper SIGCHLD
         handling. :func:`patch_all` calls both by default.
    
    .. caution:: For proper SIGCHLD handling, you must yield to the event loop.
         Using :func:`patch_all` is the easiest way to ensure this.
    
    .. seealso:: :mod:`gevent.signal`
    """
    patch_module("signal")


def _check_repatching(**module_settings):
    _warnings = []
    key = "_gevent_saved_patch_all"
    if saved.get(key, module_settings) != module_settings:
        _queue_warning(
            "Patching more than once will result in the union of all True parameters being patched",
            _warnings,
        )
    first_time = key not in saved
    saved[key] = module_settings
    return (_warnings, first_time)


def patch_all(
    socket=True,
    dns=True,
    time=True,
    select=True,
    thread=True,
    os=True,
    ssl=True,
    httplib=False,
    subprocess=True,
    sys=False,
    aggressive=True,
    Event=False,
    builtins=True,
    signal=True,
):
    """
    Do all of the default monkey patching (calls every other applicable
    function in this module).
    
    .. versionchanged:: 1.1
       Issue a :mod:`warning <warnings>` if this function is called multiple times
       with different arguments. The second and subsequent calls will only add more
       patches, they can never remove existing patches by setting an argument to ``False``.
    .. versionchanged:: 1.1
       Issue a :mod:`warning <warnings>` if this function is called with ``os=False``
       and ``signal=True``. This will cause SIGCHLD handlers to not be called. This may
       be an error in the future.
    """
    _warnings, first_time = _check_repatching(**locals())
    if not _warnings:
        if not first_time:
            return
        if os:
            patch_os()
        if time:
            patch_time()
        if thread:
            patch_thread(Event=Event, _warnings=_warnings)
        if sys:
            patch_sys()
        if socket:
            patch_socket(dns=dns, aggressive=aggressive)
        if select:
            patch_select(aggressive=aggressive)
        if ssl:
            patch_ssl()
        if httplib:
            raise ValueError(
                "gevent.httplib is no longer provided, httplib must be False"
            )
        if subprocess:
            patch_subprocess()
        if builtins:
            patch_builtins()
        if signal:
            if not os:
                _queue_warning(
                    "Patching signal but not os will result in SIGCHLD handlers installed after this not being called and os.waitpid may not function correctly if gevent.subprocess is used. This may raise an error in the future.",
                    _warnings,
                )
            patch_signal()
        _process_warnings(_warnings)


def main():
    args = {}
    argv = sys.argv[1:]
    verbose = False
    script_help, patch_all_args, modules = _get_script_help()
    while argv:
        if argv[0].startswith("--"):
            option = argv[0][2:]
            if option == "verbose":
                verbose = True
            else:
                if option.startswith("no-"):
                    pass
            if option.replace("no-", "") in patch_all_args:
                args[option[3:]] = False
            if option in patch_all_args:
                args[option] = True
                if option in modules:
                    for module in modules:
                        args.setdefault(module, False)

                else:
                    sys.exit(script_help + "\n\n" + "Cannot patch %r" % option)
                del argv[0]

    if verbose:
        import pprint, os

        print(
            "gevent.monkey.patch_all(%s)"
            % (", ").join(("%s=%s" % item for item in args.items()))
        )
        print("sys.version=%s" % (sys.version.strip().replace("\n", " "),))
        print("sys.path=%s" % pprint.pformat(sys.path))
        print("sys.modules=%s" % pprint.pformat(sorted(sys.modules.keys())))
        print("cwd=%s" % os.getcwd())
    patch_all(**args)
    if argv:
        sys.argv = argv
        __package__ = None
        if not __package__ is None:
            raise AssertionError
        globals()["__file__"] = sys.argv[0]
        globals()["__package__"] = None
        with open(sys.argv[0]) as (f):
            exec(f.read(), globals())
    else:
        print(script_help)


def _get_script_help():
    from inspect import getargspec

    patch_all_args = getargspec(patch_all)[0]
    modules = [x for x in patch_all_args if "patch_" + x in globals()]
    script_help = "gevent.monkey - monkey patch the standard modules to use gevent.\n\nUSAGE: python -m gevent.monkey [MONKEY OPTIONS] script [SCRIPT OPTIONS]\n\nIf no OPTIONS present, monkey patches all the modules it can patch.\nYou can exclude a module with --no-module, e.g. --no-thread. You can\nspecify a module to patch with --module, e.g. --socket. In the latter\ncase only the modules specified on the command line will be patched.\n\nMONKEY OPTIONS: --verbose %s" % (
        ", "
    ).join(
        ("--[no-]%s" % m for m in modules)
    )
    return (script_help, patch_all_args, modules)


main.__doc__ = _get_script_help()[0]
if __name__ == "__main__":
    main()
